﻿Imports Entidad
Imports System.Data
Imports System.Data.SqlClient

Public Class DocumentoAlmacenDatos

    Sub registrarDocumentoAlmacen(da As DocumentoAlmacen)
        Dim conex As New Conexion
        Dim coneccion As New SqlConnection
        Try
            coneccion = conex.conectar()
            coneccion.Open()

            Dim query As String = "INSERT INTO Documento_Almacen(CIA,SUC_Codigo,TDOC_Codigo, " &
                                  "DCA_Codigo,DCA_FechaDocumento,EST_Codigo) " &
                                  "VALUES(@cia,@suc,@tipo,@dcaCodigo,GETDATE(),@estado)"

            Dim comando As New SqlCommand
            With comando
                .Connection = coneccion
                .CommandText = query
                .Parameters.AddWithValue("@cia", da.CIA)
                .Parameters.AddWithValue("@suc", da.SUC_Codigo)
                .Parameters.AddWithValue("@tipo", da.TDOC_Codigo)
                .Parameters.AddWithValue("@dcaCodigo", da.DCA_Codigo)
                .Parameters.AddWithValue("@estado", da.EST_Codigo)
                .ExecuteNonQuery()
            End With

        Catch ex As SqlException
            MsgBox("La conexión no está disponible.")
        Catch e As Exception
            MsgBox(e.Message)
        Finally
            coneccion.Close()
        End Try
    End Sub

    Sub actualizarEstadoDocumentoAlmacen(cia As String, suc As String, tdoc As String, codDocumento As String, estado As String)
        Dim conex As New Conexion
        Dim coneccion As New SqlConnection
        Try
            coneccion = conex.conectar()
            coneccion.Open()

            Dim query As String = "UPDATE Documento_Almacen " &
                                  "SET EST_Codigo = @estado " &
                                  "WHERE CIA = @cia AND SUC_Codigo = @suc " &
                                  "AND TDOC_Codigo = @tipoD AND DCA_Codigo = @dcaCodigo"

            Dim comando As New SqlCommand
            With comando
                .Connection = coneccion
                .CommandText = query
                .Parameters.AddWithValue("@estado", estado)
                .Parameters.AddWithValue("@cia", cia)
                .Parameters.AddWithValue("@suc", suc)
                .Parameters.AddWithValue("@tipoD", tdoc)
                .Parameters.AddWithValue("@dcaCodigo", codDocumento)
                .ExecuteNonQuery()
            End With

        Catch ex As SqlException
            MsgBox("La conexión no está disponible.")
        Catch e As Exception
            MsgBox(e.Message)
        Finally
            coneccion.Close()
        End Try
    End Sub

    Function generarCodigoDocumentoAlmacen(tdoc As String) As Integer
        Dim codigo As Integer
        Dim conex As New Conexion
        Dim coneccion As New SqlConnection
        Try
            coneccion = conex.conectar()
            coneccion.Open()

            Dim query As String = "SELECT COUNT(*) + 1 AS Contar " &
                                  "FROM Documento_Almacen " &
                                  "WHERE TDOC_Codigo = '" & tdoc & "'"

            Dim adapter As New SqlDataAdapter(query, coneccion)
            Dim ds As New DataSet
            adapter.Fill(ds)

            codigo = CInt(ds.Tables(0).Rows(0)("Contar").ToString.Trim)

        Catch ex As SqlException
            MsgBox("La conexión no está disponible.")
        Catch e As Exception
            MsgBox(e.Message)
        Finally
            coneccion.Close()
        End Try

        Return codigo
    End Function

End Class
